package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.acticle.domain.*;
import cn.tedu.boyou.acticle.service.*;
import cn.tedu.boyou.common.query.DestinationExtendQuery;
import cn.tedu.boyou.common.query.QueryObject;
import cn.tedu.boyou.common.query.StrategyCatalogQuery;
import cn.tedu.boyou.common.query.TravelQuery;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 前端目的地控制器
 */
@Controller
@RequestMapping("destination")
public class DestinationContrller {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestThemeService destThemeService;
    @Reference
    private IDestExtendService destExtendService;

    @RequestMapping("")
    public String index(Model model){
        //hotRegions  由于未拿到数据暂时关闭  没有数据影响前台展示  所以先搞个假数据
        //List<Region> list = regionService.queryHotRegion();

        //List<DestTheme> themes = destThemeService.listTopTheme(4);
    	
    	DestTheme destThemeTest = new DestTheme();
    	destThemeTest.setDeep(3);
    	destThemeTest.setId(1L);
    	destThemeTest.setName("主题名DestTheme");
    	destThemeTest.setSequence(66);
    	destThemeTest.setCoverUrl("/images/local/blank.gif");
    	List<DestTheme> themes = new ArrayList<>();;
    	themes.add(destThemeTest);
    	
    	Region regionTest = new Region();
    	regionTest.setId(5L);
    	regionTest.setInfo("最有风情的城市");
    	regionTest.setName("地区名河西中心");
    	regionTest.setSn("地区编码10000");
    	regionTest.setIshot(true);
    	List<Region> list = new ArrayList<>();;
    	list.add(regionTest);
    	
        model.addAttribute("themes",themes);
        model.addAttribute("hotRegions",list);
        return "destination/index";
    }

    @RequestMapping("/getSubthemes")
    public String getDestsOfTheme(Model model,@ModelAttribute("themeId") Long themeId){

        //themes显示当前主题分类下的所有主题
        List<DestTheme> list = destThemeService.getSubthemesByThemeId(themeId);
        model.addAttribute("themes",list);

        return "destination/themeTpl";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){
        //1:查询数据
        //2:凭借html块
        //3:页面再凭借html块
        //leftDests --children
        //rightDests
        model.addAttribute("regionId",regionId);
        List<Destination> list = destinationService.queryDestByRegionId(regionId);
        int size = list.size();
        model.addAttribute("leftDests",list.subList(0,size/2 +1));
        model.addAttribute("rightDests", list.subList(size/2 +1,size));


        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String  guide(Model model,Long id){

        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);

        //dest
        model.addAttribute("dest",dest);

        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.queryCatalogByDestId(dest.getId()));
        //strategyDetails
        model.addAttribute("strategyDetails",strategyDetailService.listAll(dest.getId()));
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo")StrategyCatalogQuery qo){
        //catalogs
        List<StrategyCatalog> list = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",list);
        //detail.title
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);
        //detail
        if(catalog != null && catalog.getDetails() != null && catalog.getDetails().size() > 0){
            StrategyDetail detail = catalog.getDetails().get(0);
            StrategyContent content = strategyDetailService.getContent(detail.getId());
            detail.setStrategyContent(content);
            model.addAttribute("detail",detail);
        }
        return "destination/surveyTpl";
    }

    @Reference
    private ITravelService travelService;
    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo")TravelQuery qo){
        model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }

    @RequestMapping("/destFilter")
    public String destFilter(Model model,Long themeId){

        if (themeId != null){
            model.addAttribute("themeId",themeId);
        }

        //SuperTheme
        List<DestTheme> topThemes = destThemeService.listTopTheme(4);
        model.addAttribute("topThemes",topThemes);

        List<DestTheme> subThemes = destThemeService.listSubTheme();
        model.addAttribute("subThemes",subThemes);

        return "destination/travelFilter";
    }

    @RequestMapping("queryDests")
    public String queryDestinations(Model model, DestinationExtendQuery qo){

        List<DestinationExtend> destinations = destExtendService.queryDestsWithExtend(qo);
        PageInfo<DestinationExtend> pageInfo = destExtendService.query(qo);
        model.addAttribute("destExtend",destinations);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/destTpl";
    }

    @RequestMapping("/getDestByMonth")
    public String getDestsByMonth(Model model,Long month){
        int num = 5;
        List<DestinationExtend> destinationExtends = destExtendService.getDestByMonth(month,num);
        model.addAttribute("dests",destinationExtends);
        model.addAttribute("month",month);
        return "destination/seasonDestTpl";
    }

}
